package cn.pku.sirx.snad.ads.strategy.impl;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.util.Assert;

import cn.pku.sirx.snad.ad.model.WeightedKeyword;
import cn.pku.sirx.snad.ads.strategy.IKeywordReorderStrategy;

/**
 * @author WangXiao
 */
public class KeywordReorderStrategyImpl implements IKeywordReorderStrategy {

	@Override
	public List<WeightedKeyword> reorderKeywords(List<WeightedKeyword> keywords) {
		Assert.notNull(keywords);

		Collections.sort(keywords, comparator);
		System.out.println("-");
		return keywords.subList(0, REORDER_RESULT_COUNT + 1); // eliminate
	}

	private static final int REORDER_RESULT_COUNT = 20;

	private Comparator<WeightedKeyword> comparator = new Comparator<WeightedKeyword>() {

		@Override
		public int compare(WeightedKeyword o1, WeightedKeyword o2) {
			double tmp = o2.getWeight() - o1.getWeight();
			return tmp <= 0.001 ? 0 : (tmp > 0.0 ? 1 : -1); // desc
		}
	};

}
